package com.my.main.service.userAndRight.serviceImpl;

import com.my.main.base.mapper.BaseMapper;
import com.my.main.base.service.Impl.BaseServiceImpl;
import com.my.main.entity.userAndRight.SysDepartment;
import com.my.main.mapper.userAndRight.SysDepartmentMapper;
import com.my.main.service.userAndRight.SysDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class SysDepartmentServiceImpl extends BaseServiceImpl<SysDepartment> implements SysDepartmentService {

    @Autowired
    private SysDepartmentMapper sysDepartmentMapper;
    @Resource(name= "sysDepartmentMapper")
    public void setBaseMapper(BaseMapper<SysDepartment> mapper) {
        super.setBaseMapper(mapper);
    }


    @Override
    public List<SysDepartment> queryParentList(Map<String, Object> map) {
        return sysDepartmentMapper.queryParentList(map);
    }

    @Override
    public List<String> queryDepartmentId(String departmentId) {
        Map<String, Object> resMap = new HashMap<>();
//
//        // 判断部门是否存在
//        if (map.get("departmentId")==null || "".equals(String.valueOf(map.get("departmentId")).trim())) {
//            return new Result(ResultCode.SUCCESS, "系统繁忙，请稍后...");
//        }

        List<String> depIds = new ArrayList<>();
        depIds.add(departmentId);
        List<String> paramList = new ArrayList<>();
        paramList.add(departmentId);
        depIds = queryDepartmentId(paramList);

        // 集合顺序反转
        Collections.reverse(depIds);

        return depIds;
    }
    // 查询部门id集合
    private List<String> queryDepartmentId(List<String> departmentId) {
        String depId = departmentId.get(departmentId.size()-1);

        SysDepartment  sysDepartment = sysDepartmentMapper.findByUuid(depId);
        if (sysDepartment!=null && !"0".equals(sysDepartment.getPid())) {
            departmentId.add(sysDepartment.getPid());
            return queryDepartmentId(departmentId);
        }

        return departmentId;
    }
}
